package com.shidaimanong.demo.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class ChatClientDemo {
    public static void main(String[] args) throws IOException {
        String serverAddress = "localhost";//服务器地址
        int serverPort = 7788;//服务器端口
        InetAddress address = InetAddress.getByName(serverAddress);

        while (true) {
            System.out.println("请输入你想说的话：");
            Scanner scanner = new Scanner(System.in);
            String info = scanner.nextLine();
            if (info.isEmpty()) {
                break;
            }
            byte[] data = info.getBytes();
            // 1.创建数据包，包含发送的消息
            DatagramPacket requestPacket = new DatagramPacket(data, data.length, address, serverPort);
            // 2.创建DatagramSocket对象
            DatagramSocket socket = new DatagramSocket();
            // 3.向服务器端发送消息
            socket.send(requestPacket);
        }
    }
}
